package com.lzy.okgo.harmony;

import com.lzy.okgo.OkGo;
import ohos.aafwk.ability.AbilityPackage;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import okhttp3.internal.Util;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MyApplication extends AbilityPackage {
    @Override
    public void onInitialize() {
        super.onInitialize();
        OkGo okGo = OkGo.getInstance();
        // 自定义线程池
        ThreadPoolExecutor okHttpDispatcher = new ThreadPoolExecutor(
                0,
                Integer.MAX_VALUE,
                60L,
                TimeUnit.SECONDS,
                new SynchronousQueue<>(),
                Util.threadFactory("OkHttp Dispatcher", false)
        );
        // 使用OkGo的配置，重新创建一个OkHttpClient
        OkHttpClient okHttpClient = okGo.getOkHttpClient()
                .newBuilder()
                .dispatcher(new Dispatcher(okHttpDispatcher))
                .build();
        // 重新配置OkHttpClient
        okGo.setOkHttpClient(okHttpClient)
                .init(this);
    }
}